﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security.Cryptography;
using parkMonitor.DataBase;

namespace parkMonitor.manualParking
{
    public partial class Register : Form
    {
        public Register()
        {
            InitializeComponent();           
        }

        private void register_now_Click(object sender, EventArgs e)
        {
            int flag = 0;
            string tel = this.textBox1.Text;
            if (tel.Length != 11)
            {
                MessageBox.Show("电话号码不合法");
            }
            else
            {
                flag = flag + 1;
            }
            byte[] passwordText = Encoding.Default.GetBytes(this.textBox2.Text.Trim());
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] passwordMD5 = md5.ComputeHash(passwordText);
            string password = BitConverter.ToString(passwordMD5).Replace("-", "");
            if ((passwordText.Length < 6 || passwordText.Length > 20) && flag >= 1)
            {
                MessageBox.Show("密码格式不正确,请填写6-20位数字或字母");
            }
            else
            {
                flag = flag + 1;
            }
            byte[] passwordSureText = Encoding.Default.GetBytes(this.textBox3.Text.Trim());
            byte[] passwordSureMD5 = md5.ComputeHash(passwordSureText);
            string passwordSure = BitConverter.ToString(passwordSureMD5).Replace("-", "");
            if (!password.Equals(passwordSure) && flag >= 2)
            {
                MessageBox.Show("两次密码不一样");
            }
            else
            {
                flag = flag + 1;
            }
            //DBLocationOperator locationOper = new DBLocationOperator();
            DBOperation oper = new DBOperation();
            string connectionStr = "SqlConnectionLocation";
            bool isTelRegister = oper.IsTelRegister(connectionStr,tel);
            if (isTelRegister&&flag >= 3)
            {
                MessageBox.Show("此电话号码已被注册");
            }
            else
            {
                flag = flag + 1;               
            }
            int userID = oper.InsertUser(connectionStr,tel, password);
            if (userID!=0&&flag >= 4)
            {
                MessageBox.Show("注册成功");
                this.Close(); 
            }
        }
    }
}
